package kamikaze.service;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ErrorHandler {

	private Collection<String> errorList;

	public ErrorHandler(Collection<String> errorList) {
		this.errorList = errorList;
	}

	public ErrorHandler() {
		errorList = new LinkedList<String>();
	}

	public void addErrorMessageIfNotNull(String message) {
		if (message != null) {
			errorList.add(message);
		}
	}

	public boolean handle(HttpServletRequest req, HttpServletResponse resp) throws
			ServletException, IOException {

		if (!errorList.isEmpty()) {
			req.setAttribute("errorList", errorList);
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			return true;
		} else {
			return false;
		}

	}
}
